<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useAuthStore } from '@/stores/auth.store'
import { navigateTo } from '#app'

definePageMeta({
  middleware: 'auth' // 需要登录才能访问
})

const authStore = useAuthStore()
const messages = ref<Array<{id: number, text: string, sender: string, timestamp: Date}>>([])
const newMessage = ref('')

// 检查登录状态
onMounted(() => {
  if (!authStore.isLoggedIn) {
    navigateTo('/login')
  }
})

// 发送消息
const sendMessage = () => {
  if (newMessage.value.trim()) {
    messages.value.push({
      id: Date.now(),
      text: newMessage.value,
      sender: authStore.user?.username || '用户',
      timestamp: new Date()
    })
    newMessage.value = ''
  }
}

// 退出登录
const handleLogout = () => {
  authStore.logout()
  navigateTo('/login')
}
</script>

<template>
  <div class="chat-container">
    <!-- 头部 -->
    <header class="chat-header">
      <h1 class="text-xl font-semibold">HmChat</h1>
      <div class="user-info">
        <span class="text-sm text-gray-600">欢迎, {{ authStore.user?.username }}</span>
        <button @click="handleLogout" class="logout-btn">
          退出
        </button>
      </div>
    </header>

    <!-- 消息区域 -->
    <main class="messages-container">
      <div v-if="messages.length === 0" class="empty-state">
        <p class="text-gray-500">暂无消息，开始聊天吧！</p>
      </div>
      <div v-else class="messages-list">
        <div 
          v-for="message in messages" 
          :key="message.id" 
          class="message-item"
        >
          <div class="message-header">
            <span class="sender">{{ message.sender }}</span>
            <span class="timestamp">{{ message.timestamp.toLocaleTimeString() }}</span>
          </div>
          <div class="message-content">
            {{ message.text }}
          </div>
        </div>
      </div>
    </main>

    <!-- 输入区域 -->
    <footer class="input-container">
      <form @submit.prevent="sendMessage" class="input-form">
        <input
          v-model="newMessage"
          type="text"
          placeholder="输入消息..."
          class="message-input"
        />
        <button 
          type="submit" 
          :disabled="!newMessage.trim()"
          class="send-btn"
        >
          发送
        </button>
      </form>
    </footer>
  </div>
</template>

<style scoped>
.chat-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 800px;
  margin: 0 auto;
  background: white;
  border: 1px solid #e5e7eb;
}

.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  border-bottom: 1px solid #e5e7eb;
  background: #f9fafb;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.logout-btn {
  padding: 0.5rem 1rem;
  background: #ef4444;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.875rem;
}

.logout-btn:hover {
  background: #dc2626;
}

.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
}

.empty-state {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}

.messages-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.message-item {
  padding: 0.75rem;
  background: #f3f4f6;
  border-radius: 8px;
}

.message-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
}

.sender {
  font-weight: 600;
  color: #374151;
}

.timestamp {
  font-size: 0.75rem;
  color: #6b7280;
}

.message-content {
  color: #111827;
}

.input-container {
  padding: 1rem;
  border-top: 1px solid #e5e7eb;
  background: #f9fafb;
}

.input-form {
  display: flex;
  gap: 0.5rem;
}

.message-input {
  flex: 1;
  padding: 0.75rem;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  outline: none;
}

.message-input:focus {
  border-color: #3b82f6;
  box-shadow: 0 0 0 1px #3b82f6;
}

.send-btn {
  padding: 0.75rem 1.5rem;
  background: #3b82f6;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-weight: 500;
}

.send-btn:hover:not(:disabled) {
  background: #2563eb;
}

.send-btn:disabled {
  background: #9ca3af;
  cursor: not-allowed;
}
</style>